﻿if (typeof Util == 'undefined') { var Util = {}; }

Util.pluginDefaultBehavior = function (plugin, pluginName, args, contructorFunc) {
    if (!plugin.length)
        return plugin;

    var options = args[0];
    var args = Array.prototype.slice.call(args, 1);

    if (typeof options == 'string') {
        var obj = $(plugin[0]).data(pluginName + "Data");

        if (obj && obj[options])
            return obj[options].apply(plugin, args);
        else if (console)
            console.log("Unable to find method " + options + " in element " + pluginName);
    }
    else
        return plugin.each(function () {

            var obj = $(this).data(pluginName + "Data");

            if (obj) {
                $(this).removeData(pluginName + "Data");

            }
            obj = contructorFunc();
            $(this).data(pluginName + "Data", obj);

            obj.initialize(this, options);
        });
};

Util.Clone = function (obj) {

    if (typeof obj !== 'object' || obj == null) {
        return obj;
    }

    var c = obj instanceof Array ? [] : {};

    for (var i in obj) {
        var prop = obj[i];

        if (typeof prop == 'object') {
            if (prop instanceof Array) {
                c[i] = [];

                for (var j = 0; j < prop.length; j++) {
                    if (typeof prop[j] != 'object') {
                        c[i].push(prop[j]);
                    } else {
                        c[i].push(Util.Clone(prop[j]));
                    }
                }
            } else if (prop instanceof Date) {
                c[i] = new Date(prop.getTime());
            } else {
                c[i] = Util.Clone(prop);
            }
        } else {
            c[i] = prop;
        }
    }

    return c;
};

Util.stringifyJSON = function (obj) {
    return JSON2 ? JSON2.stringify(obj) : JSON ? JSON.stringifyWcf(obj) : "";
};

Util.isNumber = function (o) {
    return typeof o == "number";
};

Util.isValidNumber = function (n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
};

Util.isString = function (o) {
    return typeof o == "string";
};

Util.isDate = function (o) {
    return o.getDay && typeof o.getDay == "function";
};

Util.isBool = function (o) {
    return typeof o == "boolean";
};

Util.UpdateTips = function (t, tipComponent) {
    $(".validateTips", component).text("");
    if (tipComponent != undefined) {
        if (t != undefined && t.length > 0) {
            $(".ui-state-highlight", component).removeClass("ui-state-highlight");
            tipComponent.text(t).addClass("ui-state-highlight");
        }
        else
            $(".ui-state-highlight", component).removeClass("ui-state-highlight");
        //tipComponent.text(t).removeClass("ui-state-highlight");
    }
};

Util.ErrorHighlight = function (component, action) {
    if (action == EnumHelper.Error.Action.Remove)
        component.removeClass("ui-state-error");
    else
        component.addClass("ui-state-error");
};

Util.CheckLength = function (obj) {
    if (obj.Min > 0 && obj.Max > 0 && (obj.Component.val().length > obj.Max || obj.Component.val().length < obj.Min))
        return { Success: false, ErrorMessage: "Tamanho do campo " + obj.Name + " deve ter entre " + obj.Min + " e " + obj.Max + " caracteres.", Component: obj.Component };
    else
        return { Success: true, ErrorMessage: "", Component: obj.Component };
};

Util.CheckRegexp = function (component, regexp, n, tipComponent) {
    if (regexp != undefined && !regexp.test(component.val()))
        return false;
    else
        return true;
};

Util.IsEmail = function (obj) {
    if (!obj.Component.val().isEmail())
        return { Success: false, ErrorMessage: "Campo " + obj.Name + " está incorreto.", Component: obj.Component };
    else
        return { Success: true, ErrorMessage: "", Component: obj.Component };
};

Util.Error = function (objs, componentToShowError) {

    var errors = [];

    for (var i = 0; i < objs.length; i++) {
        if (objs[i].Success) {
            Util.ErrorHighlight(objs[i].Component, EnumHelper.Error.Action.Remove);
        }
        else {
            Util.ErrorHighlight(objs[i].Component, EnumHelper.Error.Action.Add);

            errors.push(objs[i]);
            errors[0].Component.focus();
        }
    }

    Util.UpdateTips(componentToShowError, errors);
};

Util.UpdateTips = function (componentToShowError, errors) {

    if (componentToShowError != undefined) {
        if (!errors || errors.length == 0)
            componentToShowError.hide();
        else
            componentToShowError.show();

        var html = "<ul>";

        for (var i = 0; i < errors.length; i++) {
            var error = errors[i];

            html += "<li>" + error.ErrorMessage + "</li>";
        }

        html += "</ul>";

        componentToShowError.html(html);
    }
};

Util.Success = function (obj) {
    Util.ErrorHighlight(obj, EnumHelper.Error.Action.Remove);
    Util.UpdateTips("", obj.Component);
};

/*Util.UpdateTips = function (t, tipComponent) {
$(".validateTips", component).text("");
if (tipComponent != undefined) {
if (t != undefined && t.length > 0) {
$(".ui-state-highlight", component).removeClass("ui-state-highlight");
tipComponent.text(t).addClass("ui-state-highlight");
}
else
$(".ui-state-highlight", component).removeClass("ui-state-highlight");
//tipComponent.text(t).removeClass("ui-state-highlight");
}
}

Util.ErrorHighlight = function (obj, action) {
if (action == EnumHelper.Error.Action.Remove)
obj.Component.removeClass("ui-state-error");
else
obj.Component.addClass("ui-state-error");
}

Util.CheckLength = function (obj) {
if (obj.Min > 0 && obj.Max > 0 && (obj.Component.val().length > obj.Max || obj.Component.val().length < obj.Min)) {
if (obj.Error)
obj.Error(obj);

return false;
}
else {
if (obj.Success)
obj.Success(obj);

return true;
}
};

Util.CheckRegexp = function (component, regexp, n, tipComponent) {
if (regexp != undefined) {
if (!(regexp.test(component.val()))) {
component.addClass("ui-state-error");
Util.UpdateTips(n, tipComponent);
return false;
}
else {
return true;
}
}
else
return true;
};

Util.Error = function (obj) {
Util.ErrorHighlight(obj, EnumHelper.Error.Action.Add);

obj.Component.focus();

Util.UpdateTips("Tamanho do " + obj.Name + " deve estar entre " + obj.Min + " e " + obj.Max + ".", obj.Component);
};

Util.UpdateTips = function (error, obj) {

var errorComponent = obj.Component.siblings(".Error");

if (errorComponent != undefined) {

errorComponent.text("");

if (errorComponent.length > 0) {
$(".ui-state-highlight", obj.Component).removeClass("ui-state-highlight");

errorComponent.text(t).addClass("ui-state-highlight");
}
else
errorComponent.removeClass("ui-state-highlight");
}
};

Util.Success = function (obj) {
Util.ErrorHighlight(obj, EnumHelper.Error.Action.Remove);
Util.UpdateTips("", obj.Component);
};*/